﻿/*
    File name:          Utility
    File type:          Class
    Author:             Sune Krog-Meyer 09888
    Creation date:      03-04-2011
    Description:        Contains utilityfunktions for the robot.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using i4pro_gruppe_d.ControllerCommunication;
using i4pro_gruppe_d.RobotCommunication;
using i4pro_gruppe_d.SimulatorAdaptor;
using i4pro_gruppe_d.Types;

namespace i4pro_gruppe_d.Hardware
{
    public class Utility
    {
        private readonly IControllerCommunication _utility;

        /// <summary>
        /// Classconstructor
        /// </summary>
        /// <param name="physical"> True if robot, false if simulator </param>
        public Utility(bool physical)
        {
            if(physical)
            {
                _utility = new ConcreteDLLAdaptor();     
            }
            else
            {
                _utility = new ConcreteSimulatorAdaptor();
            }
        }

        /// <summary>
        /// Invoke method to initialize the Robot and enable other funktioncalls.
        /// </summary>
        /// <param name="mode"> Should always be 1 </param>
        public void InitRobot(ushort mode = (ushort)1)
        {
            Console.WriteLine(_utility.init(mode) ? "Utility InitRobot: Success" : "Utility InitRobot: Failed");
        }

        /// <summary>
        /// Invoke method to Home the Robot. (CAUTION: Takes up to 5 minutes to complete)
        /// </summary>
        /// <param name="axis"> Axes to be homed, '&' means all </param>
        public void HomeRobot(char axis = '&')
        {
            Console.WriteLine(_utility.home(axis) ? "Utility HomeRobot: Success" : "Utilty HomeRobot: Failed" );
        }

        /// <summary>
        /// Enables movementfunktions 
        /// </summary>
        /// <param name="axis"> Axes to be controled </param>
        /// <param name="status"> True if enable, false if disable </param>
        public void SetControl(char axis, bool status)
        {
            Console.WriteLine(_utility.control(axis, status) ? "Utility SetControl: Success" : "Utility SetControl: Failed");
        }

        /// <summary>
        /// Returns if the emergencybutton has been activated
        /// </summary>
        public bool IsEmergency()
        {
            Console.WriteLine(_utility.isEmergency() ? "Utility Emergency: True" : "Utility Emergency: False");
            return _utility.isEmergency();
        }

        /// <summary>
        /// Closing the connection to the robot 
        /// </summary>
        public void CloseConnection()
        {
            _utility.closeConnection();
            Console.WriteLine("Connection Closed");
        }

        /// <summary>
        /// Creating a new robotvector for the robot, which can contain coordinatepoints
        /// </summary>
        /// <param name="group"> Which part of the robot should be controlled, should be an 'A' </param>
        /// <param name="vectorName"> Name of the robotvector </param>
        /// <param name="size"> amount of points in the vector </param>
        public void DefineVector(char group, string vectorName, short size)
        {
            Console.WriteLine(_utility.defineVector(group, vectorName, size) ? "Utility DefineVector: Succes" : "Utility DefineVector: Failed");
        }

        /// <summary>
        /// Adds information from a Vectorobject to a coordinatepoint and saves it in a robotvector 
        /// </summary>
        /// <param name="vectorName"> Name of the robotvector </param>
        /// <param name="vector"> Vectorobject with coordinateinformations </param>
        public void AddCoordinatePoint(string vectorName, Vector vector)
        {
            Console.WriteLine(_utility.teach(vectorName, vector) ? "Utility AddCoordinatePoint: Succes" : "Utility AddCoordinatePoint: Failed");   
        }

        /// <summary>
        /// Debugfunktion to check if a connection to the robot has been established 
        /// </summary>
        public void IsRobotOnline()
        {
            Console.WriteLine(_utility.isRobotOnline() ? "Utility IsRobotOnline: Succes" : "Utility IsRobotOnline: Failed");
        }

        /// <summary>
        /// Saves the current possition in a reference vector 
        /// </summary>
        /// <param name="vector"> Vectorobject with coordinateinformations </param>
        public void GetCurrentPos(ref Vector vector)
        {
            Console.WriteLine(_utility.getCurrentPos(ref vector) ? "Utility getCurrentPos: Succes" : "Utility getCurrentPos: Failed");
        }
    }
}
